//
//  MyAttractOfislanViewController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/14.
//  Copyright © 2016年 game. All rights reserved.

#import "MyAttractOfislanViewController.h"
#import "MyAttractOfislanCellTableViewCell.h"
#import "MyAttractOfislanModel.h"
#import "IslanDescribCtll.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface MyAttractOfislanViewController ()<UITableViewDataSource,UITableViewDelegate,MyAttractOfislanCellTableViewCellDeleget>
@property (weak, nonatomic) IBOutlet UILabel *navTitle;
@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,strong) MyAttractOfislanModel *attraModel;
@property (nonatomic,assign) NSInteger nextpage;
@property (nonatomic,strong) NSMutableArray *dataAry;
@end

@implementation MyAttractOfislanViewController
-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.app.myTabBar.tabBar.hidden = YES;
    StatusBlack;
    if ([self.IsYujuDian isEqualToString:@"YES"]) {
        self.navTitle.text = @"我关注的渔具店";
    }
}

- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = tabGropBackColor;
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableview.backgroundColor = tabGropBackColor;
    self.tableview.separatorStyle = 0;
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerNib:[UINib nibWithNibName:@"MyAttractOfislanCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    self.tableview.tableFooterView = [UIView new];
    self.tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];
    self.nextpage = 2;
    __weak typeof(self) weakself = self;
    _tableview.TapBlok = ^{ [weakself setUrl:weakself.Url]; };

    
}

-(void)setUrl:(NSString *)Url{
    _Url = Url;
    [self getDataWhithPage:1];
}
-(void)getDataWhithPage:(NSInteger )page{

    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:self.Url,page]AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (!error) {
            
            self.attraModel = [[MyAttractOfislanModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
            [self.dataAry addObjectsFromArray:self.attraModel.list];
            self.tableview.isShowImage = YES;
            [self.tableview reloadData];
            
            [_tableview.footer endRefreshing];
        }else{
            self.tableview.isShowFile = YES;
            [self.tableview reloadData];
            
            [_tableview.footer endRefreshing];
        }
    }];
}
-(void)RefreshFooter{
    
    SpacePagerModel *pageModel  = self.attraModel.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.tableview.footer noticeNoMoreData];
    }else
        [self getDataWhithPage:self.nextpage];
    self.nextpage ++;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

//    NSLog(@"%ld",self.attraModel.my_attention_diaochang_list.count);
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return SCREEN_HEIGHT*0.22;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyAttractOfislanCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataAry[indexPath.row];
    
    if (self.IsYujuDian) {
        cell.module = @"dianpu";
    }else{
        cell.module = @"diaochang";

    }
    
    cell.deleget = self;
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAttractOfislanCellTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    IslanDescribCtll *desCreib = [IslanDescribCtll new];
    desCreib.isYuJuDian = self.IsYujuDian;
    desCreib.detail = cell.detail;
    desCreib.latitude = cell.latitude;
    desCreib.longitude = cell.longitude;
//    desCreib.isYuJuDian = self.segment.selectedSegmentIndex;
    desCreib.localtion = cell.localtion;
    desCreib.ID = cell.tid;
    
   
    [self.navigationController pushViewController:desCreib animated:YES];
}

#pragma mark---点击取消代理方法
-(void)CancelClickWhithCell:(UITableViewCell *)cell{

    NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
    
    [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
    [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
    if ((indexPath.row+1)>=self.dataAry.count) {
        [self getDataWhithPage:self.nextpage];
    }

}

@end